package com.huixuebao.after.service.impl;


import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.huixuebao.after.dto.BaseDivDTO;
import com.huixuebao.after.entity.BaseDivisionEntity;
import com.huixuebao.after.mapper.BaseDivisionMapper;
import com.huixuebao.after.service.BaseDivisionService;
import com.huixuebao.common.bean.ApiResult;
import com.huixuebao.common.enumeration.ResultCodeEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class BaseDivisionServiceImpl extends ServiceImpl<BaseDivisionMapper,BaseDivisionEntity> implements BaseDivisionService {

    @Autowired
    BaseDivisionMapper baseDivisionMapper;
    @Override
    public ApiResult<List<BaseDivDTO>> getAreaCode() {
        ApiResult apiResult = new ApiResult();
        try {
            List<BaseDivisionEntity> list = this.list();
            List<BaseDivDTO> result = new ArrayList<>();
            list.stream().forEach(entity ->{
                BaseDivDTO baseDivDTO = new BaseDivDTO();
                BeanUtil.copyProperties(entity,baseDivDTO);
                result.add(baseDivDTO);
            });

            apiResult.setData(result);
            apiResult.setSuccess(true);
            apiResult.setCode(ResultCodeEnum.SUCCESS.getCode());
            apiResult.setMessage("获取地区表信息成功");
        }catch (Exception e){
            apiResult.setSuccess(false);
            apiResult.setCode(ResultCodeEnum.FAILURE.getCode());
            apiResult.setMessage("获取地区表信息失败");
        }
        return apiResult;
    }
}
